package com.example.liuke.guangguang.ui;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.example.liuke.guangguang.R;
import com.example.liuke.guangguang.videopicwebview.Getfiles;
import com.example.liuke.guangguang.videopicwebview.Getvideotime;
import com.example.liuke.guangguang.videopicwebview.WebVideoSliderView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by liuke on 2017/11/8.
 */

public class Videotwo extends AppCompatActivity {
    private SliderLayout sliderLayout;
    private ArrayList<String> videourl_list;
    private HashMap<Integer, BaseSliderView> myhasmap = new HashMap<Integer, BaseSliderView>();
    int currentPosition = -1;
    View currentSlider, view1;
    private CustomerVideoView customerVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_videopic);
        sliderLayout = (SliderLayout) findViewById(R.id.myslid);
        initAd();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sliderLayout.stopAutoCycle();
    }

    private void initAd() {
        videourl_list = Getfiles.getImagePathFromSD("bankvideo");
        //获取屏幕参数
        DisplayMetrics dMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
        int sliderLayoutWidth = dMetrics.widthPixels;
        int sliderLayoutHeight = dMetrics.heightPixels;
        RelativeLayout.LayoutParams itemViewParams = new RelativeLayout.LayoutParams(sliderLayoutWidth, sliderLayoutHeight);
        sliderLayout.setLayoutParams(itemViewParams);
        sliderLayout.removeAllSliders();
        //sliderLayout.setDuration(5000);
        sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
        sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);//设置指示器的位置
        sliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                /*Log.d("posp", position + "");
                String string = "";
                string = videourl_list.get(position);
                if (string.endsWith(".mp4")) {
                    sliderLayout.stopAutoCycle();
                    sliderLayout.startAutoCycle(Getvideotime.getvideotime(string), 3000, true);
                }*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        sliderLayout.setCustomAnimation(new DescriptionAnimation() {


            @Override
            //当前的item即将离开的时候调用
            public void onPrepareCurrentItemLeaveScreen(View currentview) {
                Log.d("onnPrepareCurrent", "onPrepareCurrent1");
                String string = "";
                currentPosition = sliderLayout.getCurrentPosition();
                currentSlider = currentview;
                if (currentPosition != -1) {
                    string = videourl_list.get(currentPosition);
                    if (string.endsWith(".mp4")) {
                        CustomerVideoView customerVideoView = (CustomerVideoView) currentSlider.findViewById(R.id.myvideo);
                        if (customerVideoView != null && customerVideoView.isPlaying()) {
                            customerVideoView.pause();
                            Toast.makeText(Videotwo.this, "前页停止播放", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }

            @Override
            public void onPrepareNextItemShowInScreen(View next) {
                //super.onPrepareNextItemShowInScreen(next);
                Log.d("onnPrepareCurrent", "onPrepareNextItemShowInScreen2");
            }

            @Override
            public void onCurrentItemDisappear(View view) {
                //super.onCurrentItemDisappear(view);
                Log.d("onnPrepareCurrent", "onCurrentItemDisappear3");
            }

            @Override//下一个item完全显示出来的时候调用onNextItemAppear
            public void onNextItemAppear(View view) {
                Log.d("onnNextItemAppear", "onNextItemAppear4");
                view1 = view;
                String string = "";
                if (currentPosition != -1) {
                    string = videourl_list.get(currentPosition);
                    if (string.endsWith(".mp4")) {
                        CustomerVideoView customerVideoView = (CustomerVideoView) currentSlider.findViewById(R.id.myvideo);
                        if (customerVideoView != null) {
                            customerVideoView.pause();
                        }
                        sliderLayout.startAutoCycle(5000, 5000, true);
                    }
                }
                currentPosition = sliderLayout.getCurrentPosition();
                currentSlider = view;

                string = videourl_list.get(currentPosition);
                if (!string.endsWith(".mp4")) {
                    sliderLayout.startAutoCycle(5000, 5000, true);
                    //sliderLayout.startAutoCycle();
                }
                if (string.endsWith(".mp4")) {
                    sliderLayout.stopAutoCycle();

                    customerVideoView = (CustomerVideoView) view1.findViewById(R.id.myvideo);
                    if (customerVideoView != null) {
                        customerVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                customerVideoView.stopPlayback();
                                sliderLayout.startAutoCycle(500, 5000, true);
                                //sliderLayout.startAutoCycle();
                            }
                        });

                        customerVideoView.setPlayPauseListener(new CustomerVideoView.PlayPauseListener() {
                            @Override
                            public void onPlay() {
                                view1.findViewById(R.id.progress1).setVisibility(View.INVISIBLE);
                            }

                            @Override
                            public void onPause() {//暂停播放监听

                            }
                        });
                        customerVideoView.start();
                        Toast.makeText(Videotwo.this, "滑动到此页使他播放", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        for (int i = 0; i < videourl_list.size(); i++) {
            if (videourl_list.get(i).endsWith(".mp4")) {
                WebVideoSliderView webVideoSliderView = new WebVideoSliderView(this);
                //webVideoSliderView.setWebVideoSliderView(this);
                webVideoSliderView.loadUrl("file://" + videourl_list.get(i));//视频的url
                sliderLayout.addSlider(webVideoSliderView);
                myhasmap.put(i, webVideoSliderView);
            } else {
                TextSliderView textSliderView = new TextSliderView(this);
                textSliderView.image("file://" + videourl_list.get(i)).setScaleType(BaseSliderView.ScaleType.Fit);//图片的url
                sliderLayout.addSlider(textSliderView);
                myhasmap.put(i, textSliderView);
            }
        }

        String stringa = "";
        stringa = videourl_list.get(0);
        if (stringa.endsWith(".mp4")) {
            sliderLayout.startAutoCycle(Getvideotime.getvideotime(stringa), 5000, true);
        } else {
            //延迟5秒之后，之后的都是五秒
            sliderLayout.startAutoCycle(5000, 5000, true);
        }
    }
}